package com.hkbigdata.datagovernance.governance.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hkbigdata.datagovernance.governance.bean.GovernanceAssessDetail;
import com.hkbigdata.datagovernance.governance.bean.GovernanceAssessGlobal;
import com.hkbigdata.datagovernance.governance.bean.GovernanceAssessTecOwner;
import com.hkbigdata.datagovernance.governance.service.GovernanceAssessDetailService;
import com.hkbigdata.datagovernance.governance.service.GovernanceAssessGlobalService;
import com.hkbigdata.datagovernance.governance.service.GovernanceAssessTecOwnerService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.awt.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/governance")
public class GovernanceAssessDetailController {
    @Autowired
    GovernanceAssessGlobalService governanceAssessGlobalService;

    @Autowired
    GovernanceAssessDetailService governanceAssessDetailService;

    @Autowired
    GovernanceAssessTecOwnerService governanceAssessTecOwnerService;
    @PostMapping("/assess/{date}")
    public String upDateAssess(@PathVariable("date") String assessDate){
        governanceAssessDetailService.mainAssess(assessDate);
        return "success";
    }

    //全局分数
    @GetMapping("globalScore")
    public String globalScore(){
//        String assessDate = DateFormatUtils.format(new Date(),"yyyy-MM-dd");
        String assessDate = "2023-1-1";
        GovernanceAssessGlobal governanceAssessGlobal = governanceAssessGlobalService.getOne(new QueryWrapper<GovernanceAssessGlobal>()
                .orderByDesc("assess_date")
                .last("limit 1")
        );

        JSONObject jsonObject  = new JSONObject();
        jsonObject.put("assessDate", assessDate);
        jsonObject.put("sumScore", governanceAssessGlobal.getScore());

        //擦混关键list集合来存储五个维度的分数
        ArrayList<BigDecimal> scoreList = new ArrayList<>();
        scoreList.add(governanceAssessGlobal.getScoreSpec());
        scoreList.add(governanceAssessGlobal.getScoreStorage());
        scoreList.add(governanceAssessGlobal.getScoreCalc());
        scoreList.add(governanceAssessGlobal.getScoreQuality());
        scoreList.add(governanceAssessGlobal.getScoreSecurity());

        jsonObject.put("scoreList", scoreList);

        return jsonObject.toJSONString();
    }

    //考评问题明细
    @GetMapping("/problemList/{type}/{pageNo}/{pageSize}")
    public String problemList(@PathVariable("type") String governanceType,
                              @PathVariable("pageNo") Integer pageNo,
                              @PathVariable("pageSize") Integer pageSize
    ){
        Integer from = (pageNo - 1) * pageSize;

        java.util.List<GovernanceAssessDetail> governanceAssessDetailList = governanceAssessDetailService.list(new QueryWrapper<GovernanceAssessDetail>()
                .eq("governance_type",governanceType)
                .lt("assess_score",10)
                .inSql("assess_date","select max(assess_date) from governance_assess_detail")
                .last("limit " + from + "," + pageSize)
        );

        return JSON.toJSONString(governanceAssessDetailList);
    }

    @GetMapping("/problemNum")
    public String problemNum(){

        java.util.List<Map<String, Object>> mapList = governanceAssessDetailService.listMaps(new
                QueryWrapper<GovernanceAssessDetail>()
                .select("governance_type","count(*) as ct")
                .lt("assess_score", 10)
                .inSql("assess_date", "select max(assess_date) from governance_assess_detail")
                .last("group by governance_type")
        );

        HashMap<String, Object> resultMap = new HashMap<>();

        for(Map<String, Object> map:mapList){
            resultMap.put((String) map.get("governance_type"),map.get("ct"));
        }
        return JSON.toJSONString(resultMap);


    }

    //分组人员排行榜
    @GetMapping("/rankList")
    public String rankList(){
        List<Map<String, Object>> mapList = governanceAssessTecOwnerService.listMaps(
                new
                QueryWrapper<GovernanceAssessTecOwner>()
                .select("if(tec_owner is null,'未分配',tec_owner) as tecOwner", "score")
                .inSql("assess_date", "select max(assess_date) from governance_assess_tec_owner")
                .orderByDesc("score"));

        return JSON.toJSONString(mapList);
    }
}